package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.ScrappedLine;
import com.zyd.shiro.business.service.PakScrappedLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakScrappedLine;
import com.zyd.shiro.persistence.mapper.PakScrappedLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakScrappedLineServiceImpl implements PakScrappedLineService {

    @Autowired
    private PakScrappedLineMapper pakScrappedLineMapper;

    @Override
    public ScrappedLine insert(ScrappedLine scrappedLine) {
        Assert.notNull(scrappedLine, "对象不可为空！");
        scrappedLine.getPakScrappedLine().setUpdateTime(new Date());
        scrappedLine.getPakScrappedLine().setCreateTime(new Date());
        pakScrappedLineMapper.insertSelective(scrappedLine.getPakScrappedLine());
        return scrappedLine;
    }

    @Override
    public void insertList(List<ScrappedLine> scrappedLines) {
            for(ScrappedLine scrappedLine:scrappedLines){
               scrappedLine.getPakScrappedLine().setUpdateTime(new Date());
               scrappedLine.getPakScrappedLine().setCreateTime(new Date());
               pakScrappedLineMapper.insertSelective(scrappedLine.getPakScrappedLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakScrappedLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ScrappedLine scrappedLine) {
        Assert.notNull(scrappedLine, "对象不可为空！");
        scrappedLine.setUpdateTime(new Date());
        return pakScrappedLineMapper.updateByPrimaryKey(scrappedLine.getPakScrappedLine()) > 0;
    }

    @Override
    public boolean updateSelective(ScrappedLine scrappedLine) {
        Assert.notNull(scrappedLine, "对象不可为空！");
        scrappedLine.setUpdateTime(new Date());
        return pakScrappedLineMapper.updateByPrimaryKeySelective(scrappedLine.getPakScrappedLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public ScrappedLine getByPrimaryKey(Long primaryKey) {
        PakScrappedLine pakScrappedLine = pakScrappedLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakScrappedLine ? null : new ScrappedLine(pakScrappedLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public ScrappedLine getOneByEntity(ScrappedLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakScrappedLine pakScrappedLine = pakScrappedLineMapper.selectOne(test.getPakScrappedLine());
        return null == pakScrappedLine ? null : new ScrappedLine(pakScrappedLine);
    }

    @Override
    public List<ScrappedLine> listAll() {
        List<PakScrappedLine> pakScrappedLines = pakScrappedLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakScrappedLines)) {
            return null;
        }
        List<ScrappedLine> scrappedLines = new ArrayList<>();
        for (PakScrappedLine pakScrappedLine : pakScrappedLines) {
            scrappedLines.add(new ScrappedLine(pakScrappedLine));
        }
        return scrappedLines;
    }

    @Override
    public List<ScrappedLine> listByEntity(ScrappedLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<ScrappedLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakScrappedLine> pakScrappedLines = pakScrappedLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakScrappedLines)) {
            return null;
        }
        List<ScrappedLine> scrappedLines = new ArrayList<>();
        for (PakScrappedLine pakScrappedLine : pakScrappedLines) {
            scrappedLines.add(new ScrappedLine(pakScrappedLine));
        }
        PageInfo bean = new PageInfo<PakScrappedLine>(pakScrappedLines);
        bean.setList(scrappedLines);
        return bean;
    }
    @Override
    public void increase(PakScrappedLine pakScrappedLine) {
        pakScrappedLineMapper.insert(pakScrappedLine);
    }

    @Override
    public List<PakScrappedLine> findByScrapped(Long scrappedId) {
        return pakScrappedLineMapper.findByScrapped(scrappedId);
    }


}
